﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Data;
using System.IO;
using Infragistics.Windows.Ribbon;
using System.Windows.Controls;
using Infragistics.Windows.OutlookBar;
using PTExpress.Templates;
using System.Windows.Documents;

namespace PTExpress.SysLib
{

        #region <Class SysObj> 'Các Hàm Cơ Bản'
        static public class SysObj
        {
            //Property
            #region Property
            //static private frmProgressBar frmWaiting;
            #endregion

            #region Method

            //Tìm kiếm 1 control
            static public T FindChild<T>(DependencyObject parent, string childName)
                where T : DependencyObject
            {
                // Confirm parent and childName are valid. 
                if (parent == null) return null;

                T foundChild = null;

                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    // If the child is not of the request child type child
                    T childType = child as T;
                    if (childType == null)
                    {
                        // recursively drill down the tree
                        foundChild = FindChild<T>(child, childName);

                        // If the child is found, break so we do not overwrite the found child. 
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        // If the child's name is set for search
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            //if the child's name is of the request name
                            foundChild = (T)child;
                            break;
                        }
                    }
                    else
                    {
                        // child element found.
                        foundChild = (T)child;
                        break;
                    }
                }

                return foundChild;
            }

            static public List<T> FindListChild<T>(DependencyObject parent, string childName)
                where T : DependencyObject
            {
                // Confirm parent and childName are valid. 
                if (parent == null) return null;
                List<T> result = new List<T>();
                T foundChild = null;

                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    // If the child is not of the request child type child
                    T childType = child as T;
                    if (childType == null)
                    {
                        // recursively drill down the tree
                        result.AddRange(FindListChild<T>(child, childName));

                        // If the child is found, break so we do not overwrite the found child. 
                        //if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        // If the child's name is set for search
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            //if the child's name is of the request name
                            result.Add((T)child);
                            //foundChild = (T)child;
                            //break;
                        }
                    }
                    else
                    {
                        // child element found.
                        foundChild = (T)child;
                        break;
                    }
                }

                return result;
            }


            public static T TryFindParent<T>(DependencyObject child)
                where T : DependencyObject
            {
                //get parent item
                DependencyObject parentObject = GetParentObject(child);

                //we've reached the end of the tree
                if (parentObject == null) return null;

                //check if the parent matches the type we're looking for
                T parent = parentObject as T;
                if (parent != null)
                {
                    return parent;
                }
                else
                {
                    //use recursion to proceed with next level
                    return TryFindParent<T>(parentObject);
                }
            }

            public static DependencyObject GetParentObject(DependencyObject child)
            {
                if (child == null) return null;
                ContentElement contentElement = child as ContentElement;

                if (contentElement != null)
                {
                    DependencyObject parent = ContentOperations.GetParent(contentElement);
                    if (parent != null) return parent;

                    FrameworkContentElement fce = contentElement as FrameworkContentElement;
                    return fce != null ? fce.Parent : null;
                }

                //if it's not a ContentElement, rely on VisualTreeHelper
                return VisualTreeHelper.GetParent(child);
            }
            /*
            //Load Control
            static public DataSet Preview_F12(string FrmName)
            {
                string FileName = System.Windows.Forms.Application.StartupPath + "\\LANG\\" + FrmName + ".xml";
                Waiting(100, 30);
                DataSet ds = new DataSet();
                if (!System.IO.File.Exists(FileName))
                {
                    MessageBox.Show(LanguageLib.GetLanguage().Equals("V") ? ("Không tìm thấy tập tin '" + FileName + "'") : ("File '" + FileName + "' not found!"), "QLKS", MessageBoxButton.OK, MessageBoxImage.Hand);
                    return null;
                }
                ds.ReadXml(FileName);
                frmLanguageManager frm = new frmLanguageManager();
                frm = new frmLanguageManager(ds, FileName);
                frm.ShowDialog();
                Waiting(100, 100);
                return ds;
            }
            //Show ProgressBar for Waiting
            
            #region Show ProgressBar
            static public void Waiting(long maxvalue, long value)
            {
                if (!Waiting_FirstLoad)
                {
                    Waiting_FirstLoad = true;
                    frmWaiting = new frmProgressBar();
                    frmWaiting.max = maxvalue;
                    frmWaiting.Show();
                }
                frmWaiting.SetValue(value);
                if (value == maxvalue)
                    Waiting_FirstLoad = false;
            }
            #endregion
            */
            #endregion
        }
        #endregion
    
        #region <Class LanguageLib> 'Quản Lý Đa Ngôn Ngữ'
    /*
        static public class LanguageLib
        {
            static private DataTable tb = new DataTable();
            static public bool IsRegisted = false;
            //Chọn Ngôn ngữ
            static public void LanguageChange(string id)
            {
                SysOptions.UpdateOption(Options.Language, id);
            }

            //Lấy ngôn ngữ hiện hành
            static public string GetLanguage()
            {
                string LAN = "V";
                try
                {
                    LAN = SysOptions.GetOption(Options.Language);
                }
                catch { }
                return LAN;
            }

            //Insert Xml
            //Tạo file Xml
            static public string SaveXml(string winForm, string id, string text)
            {
                if (winForm == "" || id == "" || text == "")
                    return string.Empty;

                DataSet ds = new DataSet("Form");

                //string langPath = Sm.Languages.IO.AppPath + @"\Lang\Message\";
                string langPath = System.Windows.Forms.Application.StartupPath + @"\Lang\";
                if (!Directory.Exists(langPath))
                    Directory.CreateDirectory(langPath);

                string sFileName = langPath + winForm + ".xml";

                if (!File.Exists(sFileName))
                {
                    ds.Tables.Add(new DataTable("Object"));
                    ds.Tables[0].Columns.Add("id", typeof(string));
                    ds.Tables[0].Columns.Add("vcaption", typeof(string));
                    ds.Tables[0].Columns.Add("ecaption", typeof(string));
                }
                else
                    ds.ReadXml(sFileName);

                if (ds.Tables.Count == 0)
                {
                    ds.Tables.Add(new DataTable("Object"));
                    ds.Tables[0].Columns.Add("id", typeof(string));
                    ds.Tables[0].Columns.Add("vcaption", typeof(string));
                    ds.Tables[0].Columns.Add("ecaption", typeof(string));
                }

                if (!ds.Tables[0].AsEnumerable().Any(x => x.Field<string>("id").Trim() == id))
                {
                    ds.Tables[0].Rows.Add(new string[] { id, text, "" });
                }
                ds.WriteXml(sFileName);

                return string.Empty;
            }

            //Scan control
            static public void ScanControl(string FrmName, string _name, object myVisual)
            {
                string title = string.Empty;
                string name = _name == null ? "" : _name;
                string nameControl = string.Empty;
                
                #region ScanControl
                if (myVisual is Window)
                {
                    nameControl = (myVisual as Window).Name;
                    if (!string.IsNullOrEmpty(nameControl))
                    {
                        name += (myVisual as Window).Name;
                        title = (myVisual as Window).Title;
                    }
                    //SaveXml(FrmName, name, title);
                }
                else
                    if (myVisual is MenuTool)
                    {
                        nameControl = (myVisual as MenuTool).Name;
                        if (!string.IsNullOrEmpty(nameControl))
                        {
                            name += "." + (myVisual as MenuTool).Name;
                            title = (myVisual as MenuTool).Caption;
                        }
                    }//ButtonTool
                    else
                        if (myVisual is Border)
                        {
                            nameControl = (myVisual as Border).Name;
                        }
                        else
                            if (myVisual is ButtonTool)
                            {
                                nameControl = (myVisual as ButtonTool).Name;
                                if (!string.IsNullOrEmpty(nameControl))
                                {
                                    name += "." + (myVisual as ButtonTool).Name;
                                    title = (myVisual as ButtonTool).Caption;
                                }
                            }
                            else
                                if (myVisual is RadioButtonTool)// && !(child as RadioButtonTool).Name.Equals(""))
                                {
                                    nameControl = (myVisual as RadioButtonTool).Name;
                                    if (!string.IsNullOrEmpty(nameControl))
                                    {
                                        name += "." + (myVisual as RadioButtonTool).Name;
                                        title = (myVisual as RadioButtonTool).Caption;
                                    }
                                    //SaveXml(FrmName, name, title);
                                }
                                else
                                    if (myVisual is RadioButton)// && !(child as RadioButton).Name.Equals(""))
                                    {
                                        nameControl = (myVisual as RadioButton).Name;
                                        if (!string.IsNullOrEmpty(nameControl))
                                        {
                                            name += "." + (myVisual as RadioButton).Name;
                                            title = (myVisual as RadioButton).Content.ToString();
                                        }
                                    }
                                    else
                                        if (myVisual is CheckBox)// && !(child as CheckBox).Name.Equals(""))
                                        {
                                            nameControl = (myVisual as CheckBox).Name;
                                            if (!string.IsNullOrEmpty(nameControl))
                                            {
                                                name += "." + (myVisual as CheckBox).Name;
                                                title = (myVisual as CheckBox).Content.ToString();
                                            }
                                        }
                                        else
                                            if (myVisual is Button)// && !(child as Button).Name.Equals(""))
                                            {
                                                nameControl = (myVisual as Button).Name;
                                                if (!string.IsNullOrEmpty(nameControl))
                                                {
                                                    name += "." + (myVisual as Button).Name;
                                                    if (!(myVisual as Button).Content.ToString().Contains("System.Windows.Controls.StackPanel"))
                                                        title = (myVisual as Button).Content.ToString();
                                                }
                                            }
                                            else
                                                if (myVisual is RibbonTabItem)// && !(child as RibbonTabItem).Name.Equals(""))
                                                {
                                                    nameControl = (myVisual as RibbonTabItem).Name;
                                                    if (!string.IsNullOrEmpty(nameControl))
                                                    {
                                                        name += "." + (myVisual as RibbonTabItem).Name;
                                                        title = (myVisual as RibbonTabItem).Header.ToString();
                                                    }
                                                }
                                                else
                                                    if (myVisual is GroupBox)// && !(child as GroupBox).Name.Equals(""))
                                                    {
                                                        nameControl = (myVisual as GroupBox).Name;
                                                        if (!string.IsNullOrEmpty(nameControl))
                                                        {
                                                            name += "." + (myVisual as GroupBox).Name;
                                                            title = (myVisual as GroupBox).Header.ToString();
                                                        }
                                                    }
                                                    else
                                                        if (myVisual is StackPanel)// && !(child as GroupBox).Name.Equals(""))
                                                        {
                                                            nameControl = (myVisual as StackPanel).Name;
                                                        }
                                                        else
                                                            if (myVisual is TabControl)
                                                            {
                                                                nameControl = (myVisual as TabControl).Name;
                                                            }
                                                            else
                                                                if (myVisual is TabItem)
                                                                {
                                                                    nameControl = (myVisual as TabItem).Name;
                                                                    if (!string.IsNullOrEmpty(nameControl))
                                                                    {
                                                                        name += "." + (myVisual as TabItem).Name;
                                                                        title = (myVisual as TabItem).Header.ToString();
                                                                    }
                                                                }
                                                                else
                                                                    if (myVisual is TextBlock)// && !(child as TextBlock).Name.Equals(""))
                                                                    {
                                                                        nameControl = (myVisual as TextBlock).Name;
                                                                        if (!string.IsNullOrEmpty(nameControl))
                                                                        {
                                                                            name += "." + (myVisual as TextBlock).Name;
                                                                            title = (myVisual as TextBlock).Text;
                                                                        }
                                                                    }
                                                                    else
                                                                        if (myVisual is Label)// && !(child as Label).Name.Equals(""))
                                                                        {
                                                                            nameControl = (myVisual as Label).Name;
                                                                            if (!string.IsNullOrEmpty(nameControl))
                                                                            {
                                                                                name += "." + (myVisual as Label).Name;
                                                                                title = (myVisual as Label).Content.ToString();
                                                                            }
                                                                        }
                                                                        else
                                                                            if (myVisual is MenuItem)// && !(child as MenuItem).Name.Equals(""))
                                                                            {
                                                                                nameControl = (myVisual as MenuItem).Name;
                                                                                if (!string.IsNullOrEmpty(nameControl))
                                                                                {
                                                                                    name += "." + (myVisual as MenuItem).Name;
                                                                                    title = (myVisual as MenuItem).Header.ToString();
                                                                                }
                                                                            }
                                                                            else
                                                                                if (myVisual is RibbonGroup)// && !(child as RibbonGroup).Name.Equals(""))
                                                                                {
                                                                                    nameControl = (myVisual as RibbonGroup).Name;
                                                                                    if (!string.IsNullOrEmpty(nameControl))
                                                                                    {
                                                                                        name += "." + (myVisual as RibbonGroup).Name;
                                                                                        title = (myVisual as RibbonGroup).Caption;
                                                                                    }
                                                                                }
                                                                                else
                                                                                    if (myVisual is XamOutlookBar)// && !(child as RibbonGroup).Name.Equals(""))
                                                                                    {
                                                                                        nameControl = (myVisual as XamOutlookBar).Name;
                                                                                    }
                                                                                    else
                                                                                        if (myVisual is OutlookBarGroup)// && !(child as RibbonGroup).Name.Equals(""))
                                                                                        {
                                                                                            nameControl = (myVisual as OutlookBarGroup).Name;
                                                                                            if (!string.IsNullOrEmpty(nameControl))
                                                                                            {
                                                                                                name += "." + (myVisual as OutlookBarGroup).Name;
                                                                                                if ((myVisual as OutlookBarGroup).Header != null)
                                                                                                    title = (myVisual as OutlookBarGroup).Header.ToString();
                                                                                                else
                                                                                                    title = "";
                                                                                            }
                                                                                        }
                                                                                        else
                                                                                            if (myVisual is ButtonTool)// && !(child as ButtonTool).Name.Equals(""))
                                                                                            {
                                                                                                nameControl = (myVisual as ButtonTool).Name;
                                                                                                if (!string.IsNullOrEmpty(nameControl))
                                                                                                {
                                                                                                    name += "." + (myVisual as ButtonTool).Name;
                                                                                                    title = (myVisual as ButtonTool).Caption;
                                                                                                }
                                                                                            }
                                                                                            else
                                                                                                if (myVisual is Grid)
                                                                                                {
                                                                                                    nameControl = (myVisual as Grid).Name;
                                                                                                }
                                                                                                else
                                                                                                    if (myVisual is MasterControl)
                                                                                                    {
                                                                                                        nameControl = (myVisual as MasterControl).Name;
                                                                                                        if (!string.IsNullOrEmpty(nameControl))
                                                                                                        {
                                                                                                            name += "." + (myVisual as MasterControl).Name;
                                                                                                            if ((myVisual as MasterControl).Title != null)
                                                                                                                title = (myVisual as MasterControl).Title.ToString();
                                                                                                            else
                                                                                                                title = "";
                                                                                                        }
                                                                                                    }
                                                                                                    else
                                                                                                        if (myVisual is MasterPage)
                                                                                                        {
                                                                                                            nameControl = (myVisual as MasterPage).Name;
                                                                                                            if (!string.IsNullOrEmpty(nameControl))
                                                                                                            {
                                                                                                                name += "." + (myVisual as MasterPage).Name;
                                                                                                                if ((myVisual as MasterPage).Title != null)
                                                                                                                    title = (myVisual as MasterPage).Title.ToString();
                                                                                                                else
                                                                                                                    title = "";
                                                                                                            }
                                                                                                        }
                                                                                                        else
                                                                                                            if (myVisual is PTExpress.Views.ucXToolBar)
                                                                                                            {
                                                                                                                //nameControl = (myVisual as QLKS.Views.ucXToolBar).Name;
                                                                                                                nameControl = "ucXToolBar";
                                                                                                            }
                                                                                                            else
                                                                                                                if (myVisual is ToolBar)
                                                                                                                {
                                                                                                                    //nameControl = (myVisual as ToolBar).Name;
                                                                                                                    nameControl = "ToolBar";
                                                                                                                }
                                                                                                                else
                                                                                                                    if (myVisual is Infragistics.Windows.Ribbon.RibbonWindowContentHost)
                                                                                                                    {
                                                                                                                        //nameControl = (myVisual as Infragistics.Windows.Ribbon.RibbonWindowContentHost).Name;
                                                                                                                        nameControl = "RibbonWindowContentHost";
                                                                                                                    }
                                                                                                                    else
                                                                                                                        if (myVisual is UserControl)
                                                                                                                        {
                                                                                                                            nameControl = (myVisual as UserControl).Name;
                                                                                                                        }
                                                                                                                        else
                                                                                                                            if (myVisual is Infragistics.Windows.Ribbon.XamRibbon)
                                                                                                                            {
                                                                                                                                nameControl = "XamRibbon";
                                                                                                                            }
                                                                                                                            else
                                                                                                                                if (myVisual is System.Windows.Controls.Primitives.StatusBar)
                                                                                                                                {
                                                                                                                                    nameControl = "StatusBar";
                                                                                                                                }
                                                                                                                                else
                                                                                                                                    if (myVisual is Hyperlink)
                                                                                                                                    {
                                                                                                                                        //nameControl = (myVisual as Hyperlink).Name;
                                                                                                                                        nameControl = "Hyperlink";
                                                                                                                                    }
                                                                                                                                    else
                                                                                                                                        if (myVisual is Run)
                                                                                                                                        {
                                                                                                                                            nameControl = (myVisual as Run).Name;
                                                                                                                                            if (!string.IsNullOrEmpty(nameControl))
                                                                                                                                            {
                                                                                                                                                name += "." + (myVisual as Run).Name;
                                                                                                                                                title = (myVisual as Run).Text;
                                                                                                                                            }
                                                                                                                                        }

                #endregion
                //Gọi đệ quy duyệt tất cả children
                if (string.IsNullOrEmpty(nameControl))
                    return;
                SaveXml(FrmName, name, title);
                if (myVisual is MasterControl)
                {
                    //ScanControl(FrmName, name, (myVisual as MasterControl).Title);
                    ScanControl(FrmName, name, (myVisual as MasterControl).ToolBar);
                    ScanControl(FrmName, name, (myVisual as MasterControl).Content);
                    ScanControl(FrmName, name, (myVisual as MasterControl).Footer);
                }
                if (myVisual is MasterPage)
                {
                    //ScanControl(FrmName, name, (myVisual as MasterPage).Title);
                    ScanControl(FrmName, name, (myVisual as MasterPage).ToolBar);
                    ScanControl(FrmName, name, (myVisual as MasterPage).Content);
                    ScanControl(FrmName, name, (myVisual as MasterPage).Footer);
                }
                //if (myVisual is Infragistics.Windows.DataPresenter.DataRecordPresenter)
                //{
                //    ScanControl(FrmName, name, (myVisual as Infragistics.Windows.DataPresenter.DataRecordPresenter).Record);
                //}
                //if (myVisual is Infragistics.Windows.DataPresenter.XamDataGrid)
                //{
                //    ScanControl(FrmName, name, (myVisual as Infragistics.Windows.DataPresenter.XamDataGrid).FieldLayouts);
                //}
                foreach (object child in LogicalTreeHelper.GetChildren(myVisual as DependencyObject))
                {
                    ScanControl(FrmName, name, child);
                }
            }

            //Load control
            //Scan control
            static public void LoadControl(DataTable data, string _name, object myVisual, string M_LAN)
            {
                string vtitle = string.Empty;
                string etitle = string.Empty;
                string title = string.Empty;
                string name = _name == null ? "" : _name;
                string nameControl = string.Empty;
                #region ScanControl
                if (myVisual is Window)
                {
                    nameControl = (myVisual as Window).Name;
                    if (!string.IsNullOrEmpty(nameControl))
                    {
                        name += (myVisual as Window).Name;
                        DataRow[] rows = data.Select("id like '" + name + "'");
                        if (rows.Length > 0)
                        {
                            vtitle = rows[0]["vcaption"].ToString();
                            etitle = rows[0]["ecaption"].ToString();
                            if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                            {
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(etitle))
                                    title = vtitle;
                                else
                                    title = M_LAN.Equals("V") ? vtitle : etitle;

                                (myVisual as Window).Title = title;
                            }
                        }
                    }
                    //SaveXml(FrmName, name, title);
                }
                else//MenuTool
                    if (myVisual is RadioButtonTool)// && !(child as RadioButtonTool).Name.Equals(""))
                    {
                        nameControl = (myVisual as RadioButtonTool).Name;
                        if (!string.IsNullOrEmpty(nameControl))
                        {
                            name += "." + (myVisual as RadioButtonTool).Name;
                            DataRow[] rows = data.Select("id like '" + name + "'");
                            if (rows.Length > 0)
                            {
                                vtitle = rows[0]["vcaption"].ToString();
                                etitle = rows[0]["ecaption"].ToString();
                                if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                {
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(etitle))
                                        title = vtitle;
                                    else
                                        title = M_LAN.Equals("V") ? vtitle : etitle;

                                    (myVisual as RadioButtonTool).Caption = title;
                                }
                            }
                        }
                        //SaveXml(FrmName, name, title);
                    }
                    else
                        if (myVisual is MenuTool)// && !(child as RadioButtonTool).Name.Equals(""))
                        {
                            nameControl = (myVisual as MenuTool).Name;
                            if (!string.IsNullOrEmpty(nameControl))
                            {
                                name += "." + (myVisual as MenuTool).Name;
                                DataRow[] rows = data.Select("id like '" + name + "'");
                                if (rows.Length > 0)
                                {
                                    vtitle = rows[0]["vcaption"].ToString();
                                    etitle = rows[0]["ecaption"].ToString();
                                    if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                    {
                                    }
                                    else
                                    {
                                        if (string.IsNullOrEmpty(etitle))
                                            title = vtitle;
                                        else
                                            title = M_LAN.Equals("V") ? vtitle : etitle;

                                        (myVisual as MenuTool).Caption = title;
                                    }
                                }
                            }

                        }//ButtonTool
                        else
                            if (myVisual is ButtonTool)// && !(child as RadioButtonTool).Name.Equals(""))
                            {
                                nameControl = (myVisual as ButtonTool).Name;
                                if (!string.IsNullOrEmpty(nameControl))
                                {
                                    name += "." + (myVisual as ButtonTool).Name;
                                    DataRow[] rows = data.Select("id like '" + name + "'");
                                    if (rows.Length > 0)
                                    {
                                        vtitle = rows[0]["vcaption"].ToString();
                                        etitle = rows[0]["ecaption"].ToString();
                                        if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                        {
                                        }
                                        else
                                        {
                                            if (string.IsNullOrEmpty(etitle))
                                                title = vtitle;
                                            else
                                                title = M_LAN.Equals("V") ? vtitle : etitle;

                                            (myVisual as ButtonTool).Caption = title;
                                        }
                                    }
                                }
                            }
                            else
                                if (myVisual is RadioButton)// && !(child as RadioButton).Name.Equals(""))
                                {
                                    nameControl = (myVisual as RadioButton).Name;
                                    if (!string.IsNullOrEmpty(nameControl))
                                    {
                                        name += "." + (myVisual as RadioButton).Name;
                                        DataRow[] rows = data.Select("id like '" + name + "'");
                                        if (rows.Length > 0)
                                        {
                                            vtitle = rows[0]["vcaption"].ToString();
                                            etitle = rows[0]["ecaption"].ToString();
                                            if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                            {
                                            }
                                            else
                                            {
                                                if (string.IsNullOrEmpty(etitle))
                                                    title = vtitle;
                                                else
                                                    title = M_LAN.Equals("V") ? vtitle : etitle;

                                                (myVisual as RadioButton).Content = title;
                                            }
                                        }
                                    }
                                }
                                else
                                    if (myVisual is CheckBox)// && !(child as CheckBox).Name.Equals(""))
                                    {
                                        nameControl = (myVisual as CheckBox).Name;
                                        if (!string.IsNullOrEmpty(nameControl))
                                        {
                                            name += "." + (myVisual as CheckBox).Name;
                                            DataRow[] rows = data.Select("id like '" + name + "'");
                                            if (rows.Length > 0)
                                            {
                                                vtitle = rows[0]["vcaption"].ToString();
                                                etitle = rows[0]["ecaption"].ToString();
                                                if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                {
                                                }
                                                else
                                                {
                                                    if (string.IsNullOrEmpty(etitle))
                                                        title = vtitle;
                                                    else
                                                        title = M_LAN.Equals("V") ? vtitle : etitle;

                                                    (myVisual as CheckBox).Content = title;
                                                }
                                            }
                                        }
                                    }
                                    else
                                        if (myVisual is Button)// && !(child as Button).Name.Equals(""))
                                        {
                                            nameControl = (myVisual as Button).Name;
                                            if (!string.IsNullOrEmpty(nameControl))
                                            {
                                                name += "." + (myVisual as Button).Name;
                                                if (!(myVisual as Button).Content.ToString().Contains("System.Windows.Controls.StackPanel"))
                                                {
                                                    //name += "." + (myVisual as Button).Name;
                                                    DataRow[] rows = data.Select("id like '" + name + "'");
                                                    if (rows.Length > 0)
                                                    {
                                                        vtitle = rows[0]["vcaption"].ToString();
                                                        etitle = rows[0]["ecaption"].ToString();
                                                        if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                        {
                                                        }
                                                        else
                                                        {
                                                            if (string.IsNullOrEmpty(etitle))
                                                                title = vtitle;
                                                            else
                                                                title = M_LAN.Equals("V") ? vtitle : etitle;

                                                            (myVisual as Button).Content = title;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else
                                            if (myVisual is RibbonTabItem)// && !(child as RibbonTabItem).Name.Equals(""))
                                            {
                                                nameControl = (myVisual as RibbonTabItem).Name;
                                                if (!string.IsNullOrEmpty(nameControl))
                                                {
                                                    name += "." + (myVisual as RibbonTabItem).Name;
                                                    DataRow[] rows = data.Select("id like '" + name + "'");
                                                    if (rows.Length > 0)
                                                    {
                                                        vtitle = rows[0]["vcaption"].ToString();
                                                        etitle = rows[0]["ecaption"].ToString();
                                                        if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                        {
                                                        }
                                                        else
                                                        {
                                                            if (string.IsNullOrEmpty(etitle))
                                                                title = vtitle;
                                                            else
                                                                title = M_LAN.Equals("V") ? vtitle : etitle;

                                                            (myVisual as RibbonTabItem).Header = title;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                                if (myVisual is GroupBox)// && !(child as GroupBox).Name.Equals(""))
                                                {
                                                    nameControl = (myVisual as GroupBox).Name;
                                                    if (!string.IsNullOrEmpty(nameControl))
                                                    {
                                                        name += "." + (myVisual as GroupBox).Name;
                                                        DataRow[] rows = data.Select("id like '" + name + "'");
                                                        if (rows.Length > 0)
                                                        {
                                                            vtitle = rows[0]["vcaption"].ToString();
                                                            etitle = rows[0]["ecaption"].ToString();
                                                            if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                            {
                                                            }
                                                            else
                                                            {
                                                                if (string.IsNullOrEmpty(etitle))
                                                                    title = vtitle;
                                                                else
                                                                    title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                (myVisual as GroupBox).Header = title;
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                    if (myVisual is StackPanel)// && !(child as GroupBox).Name.Equals(""))
                                                    {
                                                        nameControl = (myVisual as StackPanel).Name;
                                                    }
                                                    else
                                                        if (myVisual is TextBlock)// && !(child as TextBlock).Name.Equals(""))
                                                        {
                                                            nameControl = (myVisual as TextBlock).Name;
                                                            if (!string.IsNullOrEmpty(nameControl))
                                                            {
                                                                name += "." + (myVisual as TextBlock).Name;
                                                                DataRow[] rows = data.Select("id like '" + name + "'");
                                                                if (rows.Length > 0)
                                                                {
                                                                    vtitle = rows[0]["vcaption"].ToString();
                                                                    etitle = rows[0]["ecaption"].ToString();
                                                                    if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                    {
                                                                    }
                                                                    else
                                                                    {
                                                                        if (string.IsNullOrEmpty(etitle))
                                                                            title = vtitle;
                                                                        else
                                                                            title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                        (myVisual as TextBlock).Text = title;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                            if (myVisual is Label)// && !(child as Label).Name.Equals(""))
                                                            {
                                                                nameControl = (myVisual as Label).Name;
                                                                if (!string.IsNullOrEmpty(nameControl))
                                                                {
                                                                    name += "." + (myVisual as Label).Name;
                                                                    DataRow[] rows = data.Select("id like '" + name + "'");
                                                                    if (rows.Length > 0)
                                                                    {
                                                                        vtitle = rows[0]["vcaption"].ToString();
                                                                        etitle = rows[0]["ecaption"].ToString();
                                                                        if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                        {
                                                                        }
                                                                        else
                                                                        {
                                                                            if (string.IsNullOrEmpty(etitle))
                                                                                title = vtitle;
                                                                            else
                                                                                title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                            (myVisual as Label).Content = title;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                                if (myVisual is MenuItem)// && !(child as MenuItem).Name.Equals(""))
                                                                {
                                                                    nameControl = (myVisual as MenuItem).Name;
                                                                    if (!string.IsNullOrEmpty(nameControl))
                                                                    {
                                                                        name += "." + (myVisual as MenuItem).Name;
                                                                        DataRow[] rows = data.Select("id like '" + name + "'");
                                                                        if (rows.Length > 0)
                                                                        {
                                                                            vtitle = rows[0]["vcaption"].ToString();
                                                                            etitle = rows[0]["ecaption"].ToString();
                                                                            if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                            {
                                                                            }
                                                                            else
                                                                            {
                                                                                if (string.IsNullOrEmpty(etitle))
                                                                                    title = vtitle;
                                                                                else
                                                                                    title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                                (myVisual as MenuItem).Header = title;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                    if (myVisual is RibbonGroup)// && !(child as RibbonGroup).Name.Equals(""))
                                                                    {
                                                                        nameControl = (myVisual as RibbonGroup).Name;
                                                                        if (!string.IsNullOrEmpty(nameControl))
                                                                        {
                                                                            name += "." + (myVisual as RibbonGroup).Name;
                                                                            DataRow[] rows = data.Select("id like '" + name + "'");
                                                                            if (rows.Length > 0)
                                                                            {
                                                                                vtitle = rows[0]["vcaption"].ToString();
                                                                                etitle = rows[0]["ecaption"].ToString();
                                                                                if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                                {
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (string.IsNullOrEmpty(etitle))
                                                                                        title = vtitle;
                                                                                    else
                                                                                        title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                                    (myVisual as RibbonGroup).Caption = title;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    else
                                                                        if (myVisual is ButtonTool)// && !(child as ButtonTool).Name.Equals(""))
                                                                        {
                                                                            nameControl = (myVisual as ButtonTool).Name;
                                                                            if (!string.IsNullOrEmpty(nameControl))
                                                                            {
                                                                                name += "." + (myVisual as ButtonTool).Name;
                                                                                DataRow[] rows = data.Select("id like '" + name + "'");
                                                                                if (rows.Length > 0)
                                                                                {
                                                                                    vtitle = rows[0]["vcaption"].ToString();
                                                                                    etitle = rows[0]["ecaption"].ToString();
                                                                                    if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                                    {
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        if (string.IsNullOrEmpty(etitle))
                                                                                            title = vtitle;
                                                                                        else
                                                                                            title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                                        (myVisual as ButtonTool).Caption = title;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        else
                                                                            if (myVisual is Grid)
                                                                            {
                                                                                nameControl = (myVisual as Grid).Name;
                                                                            }
                                                                            else
                                                                                if (myVisual is MasterControl)
                                                                                {
                                                                                    nameControl = (myVisual as MasterControl).Name;
                                                                                    if (!string.IsNullOrEmpty(nameControl))
                                                                                    {
                                                                                        name += "." + (myVisual as MasterControl).Name;
                                                                                        DataRow[] rows = data.Select("id like '" + name + "'");
                                                                                        if (rows.Length > 0)
                                                                                        {
                                                                                            vtitle = rows[0]["vcaption"].ToString();
                                                                                            etitle = rows[0]["ecaption"].ToString();
                                                                                            if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                                            {
                                                                                            }
                                                                                            else
                                                                                            {
                                                                                                if (string.IsNullOrEmpty(etitle))
                                                                                                    title = vtitle;
                                                                                                else
                                                                                                    title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                                                (myVisual as MasterControl).Title = title;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                                else
                                                                                    if (myVisual is MasterPage)
                                                                                    {
                                                                                        nameControl = (myVisual as MasterPage).Name;
                                                                                        if (!string.IsNullOrEmpty(nameControl))
                                                                                        {
                                                                                            name += "." + (myVisual as MasterPage).Name;
                                                                                            DataRow[] rows = data.Select("id like '" + name + "'");
                                                                                            if (rows.Length > 0)
                                                                                            {
                                                                                                vtitle = rows[0]["vcaption"].ToString();
                                                                                                etitle = rows[0]["ecaption"].ToString();
                                                                                                if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                                                {
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (string.IsNullOrEmpty(etitle))
                                                                                                        title = vtitle;
                                                                                                    else
                                                                                                        title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                                                    (myVisual as MasterPage).Title = title;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    else
                                                                                        if (myVisual is Run)
                                                                                        {
                                                                                            nameControl = (myVisual as Run).Name;
                                                                                            if (!string.IsNullOrEmpty(nameControl))
                                                                                            {
                                                                                                name += "." + (myVisual as Run).Name;
                                                                                                DataRow[] rows = data.Select("id like '" + name + "'");
                                                                                                if (rows.Length > 0)
                                                                                                {
                                                                                                    vtitle = rows[0]["vcaption"].ToString();
                                                                                                    etitle = rows[0]["ecaption"].ToString();
                                                                                                    if (vtitle.Trim() == etitle.Trim() && vtitle.Trim() == "")
                                                                                                    {
                                                                                                    }
                                                                                                    else
                                                                                                    {
                                                                                                        if (string.IsNullOrEmpty(etitle))
                                                                                                            title = vtitle;
                                                                                                        else
                                                                                                            title = M_LAN.Equals("V") ? vtitle : etitle;

                                                                                                        (myVisual as Run).Text = title;
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                        else
                                                                                            if (myVisual is PTExpress.Views.ucXToolBar)
                                                                                            {
                                                                                                nameControl = (myVisual as PTExpress.Views.ucXToolBar).Name;
                                                                                            }
                                                                                            else
                                                                                                if (myVisual is ToolBar)
                                                                                                {
                                                                                                    nameControl = (myVisual as ToolBar).Name;
                                                                                                }
                                                                                                else
                                                                                                    if (myVisual is Infragistics.Windows.Ribbon.RibbonWindowContentHost)
                                                                                                    {
                                                                                                        nameControl = (myVisual as Infragistics.Windows.Ribbon.RibbonWindowContentHost).Name;
                                                                                                    }
                                                                                                    else
                                                                                                        if (myVisual is UserControl)
                                                                                                        {
                                                                                                            nameControl = "UserControl";
                                                                                                        }
                                                                                                        else
                                                                                                            if (myVisual is Infragistics.Windows.DataPresenter.XamDataGrid)
                                                                                                            {
                                                                                                                nameControl = (myVisual as Infragistics.Windows.DataPresenter.XamDataGrid).Name;
                                                                                                            }
                                                                                                            else
                                                                                                                if (myVisual is Infragistics.Windows.DataPresenter.GroupByArea)
                                                                                                                {
                                                                                                                    nameControl = "GroupByArea";
                                                                                                                }
                                                                                                                else
                                                                                                                    if (myVisual is Infragistics.Windows.Ribbon.XamRibbon)
                                                                                                                    {
                                                                                                                        nameControl = (myVisual as Infragistics.Windows.Ribbon.XamRibbon).Name;
                                                                                                                    }
                                                                                                                    else
                                                                                                                        if (myVisual is Border)
                                                                                                                        {
                                                                                                                            nameControl = (myVisual as Border).Name;
                                                                                                                        }
                                                                                                                        else
                                                                                                                            if (myVisual is System.Windows.Controls.Primitives.StatusBar)
                                                                                                                            {
                                                                                                                                nameControl = (myVisual as System.Windows.Controls.Primitives.StatusBar).Name;
                                                                                                                            }
                                                                                                                            else
                                                                                                                                if (myVisual is Hyperlink)
                                                                                                                                {
                                                                                                                                    nameControl = (myVisual as Hyperlink).Name;
                                                                                                                                }

                #endregion
                //Gọi đệ quy duyệt tất cả children
                //SaveXml(FrmName, name, title);
                if (string.IsNullOrEmpty(nameControl))
                    return;
                if (myVisual is MasterControl)
                {
                    LoadControl(data, name, (myVisual as MasterControl).ToolBar, M_LAN);
                    LoadControl(data, name, (myVisual as MasterControl).Content, M_LAN);
                    LoadControl(data, name, (myVisual as MasterControl).Footer, M_LAN);
                }
                if (myVisual is MasterPage)
                {
                    LoadControl(data, name, (myVisual as MasterPage).ToolBar, M_LAN);
                    LoadControl(data, name, (myVisual as MasterPage).Content, M_LAN);
                    LoadControl(data, name, (myVisual as MasterPage).Footer, M_LAN);
                }

                try
                {
                    foreach (object child in LogicalTreeHelper.GetChildren(myVisual as DependencyObject))
                    {
                        LoadControl(data, name, child, M_LAN);
                    }
                }
                catch { }
            }
        }
     * */
        #endregion
}
